package com.yuju.controller.pay;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.Result;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.enums.WxPayForScanReturnCodeEnum;
import com.yuju.pay.service.WeChatPayService;
/**
 * 
 * 类名：WechatPayController  <br />
 *
 * 功能：微信支付接口
 *
 * @author : xuehj <br />
 * @Date : 2018年2月2日 下午5:04:57  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("weChatPay")
public class WechatPayController extends BaseController {
	
	@Autowired
	private WeChatPayService wxPayService;

	/**
	 * 普通商户微信刷卡扫码支付
	 * 功能：<br/>
	 *@param  orderNo 订单号
	 *@param authCode 授权码
	 *@param totalMoney  金额
	 * @author sunyl
	 * @throws Exception 
	 * @Date 2018年1月17日 下午4:55:00 <br/>
	 */
	@RequestMapping("microPay")
	@ResponseBody
	public Result microPay(@RequestBody JSONObject jsonObject) throws Exception {
			String orderNo = jsonObject.getString("orderNo");
			String authCode = jsonObject.getString("authCode");
			String flag = jsonObject.getString("flag");
			BigDecimal totalMoney = jsonObject.getBigDecimal("totalMoney");
			/*校验*/
			Map<String, String> fail = new HashMap<>();
			if(orderNo == null || authCode == null ||totalMoney == null){
				fail.put("orderNo  authCode totalMoney", "都不能为空");
				return validateError(fail);
			}
		return  wxPayService.microPay(totalMoney, orderNo, authCode, "8.8.8.8",flag);
	}
	/**
	 * 服务商模式得刷卡支付
	 * 功能：<br/>
	 *@param  orderNo 订单号
	 *@param authCode 授权码
	 *@param totalMoney  金额
	 * @author sunyl
	 * @Date 2018年2月6日 上午9:55:45 <br/>
	 */
	@RequestMapping("microPayForService")
	@ResponseBody
	public Result microPayForService(@RequestBody JSONObject jsonObject) throws Exception {
		String orderNo = jsonObject.getString("orderNo");
		String authCode = jsonObject.getString("authCode");
		String flag = jsonObject.getString("flag");
		String hotelId = jsonObject.getString("hotelId");
		String groupId = jsonObject.getString("groupId");
		BigDecimal totalMoney = jsonObject.getBigDecimal("totalMoney");	
		/*校验*/
		Map<String, String> fail = new HashMap<>();
		if(orderNo == null || authCode == null ||totalMoney == null){
			fail.put("orderNo  authCode totalMoney", "都不能为空");
			return validateError(fail);
		}
		Result result = wxPayService.microPayForService(hotelId,groupId,totalMoney, orderNo, authCode, "8.8.8.8",flag) ;
		return  result;
	}
	/**
	 * 微信插銷訂單
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @throws Exception 
	 * @Date 2018年1月17日 下午4:55:00 <br/>
	 */
	@RequestMapping("reverse")
	@ResponseBody
	public Result reverse(@RequestBody JSONObject jsonObject) throws Exception {
		String orderNo = jsonObject.getString("orderNo");
		String authCode = jsonObject.getString("authCode");
		String flag = jsonObject.getString("flag");
		BigDecimal totalMoney = jsonObject.getBigDecimal("totalMoney");
		/*校验*/
		Map<String, String> fail = new HashMap<>();
		if(orderNo == null || authCode == null ||totalMoney == null){
			fail.put("orderNo  authCode totalMoney", "都不能为空");
			return validateError(fail);
		}
		return  wxPayService.reverse(totalMoney, orderNo, authCode, "8.8.8.8",flag);
	}
	/**
	 * 服务商退款
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2018年2月22日 上午11:42:52 <br/>
	 */
	@RequestMapping("reverseForService")
	@ResponseBody
	public Result reverseForService(@RequestBody JSONObject jsonObject) throws Exception {
		String orderNo = jsonObject.getString("orderNo");
		String hotelId = jsonObject.getString("hotelId");
		String groupId = jsonObject.getString("groupId");
		Map<String, String> fail = new HashMap<>();
		if(orderNo == null || hotelId == null ||groupId == null){
			fail.put("orderNo  hotelId groupId", "都不能为空");
			return validateError(fail);
		}
		return  wxPayService.reverseForService(orderNo,groupId,hotelId);
	}
	/**
	 * 退款
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2018年2月6日 下午5:51:11 <br/>
	 */
	@RequestMapping("refundForService")
	@ResponseBody
	public Result refundForService(@RequestBody JSONObject jsonObject) throws Exception {
		String orderNo = jsonObject.getString("orderNo");
		String outRefundNo = jsonObject.getString("outRefundNo");
		String transactionId = jsonObject.getString("transactionId");
		String flag = jsonObject.getString("flag");
		String hotelId = jsonObject.getString("hotelId");
		String groupId = jsonObject.getString("groupId");
		BigDecimal totalFee = jsonObject.getBigDecimal("totalFee");
		BigDecimal refundFee = jsonObject.getBigDecimal("refundFee");
		/*校验*/
		Map<String, String> fail = new HashMap<>();
		if(orderNo==null || transactionId==null||totalFee==null ||refundFee==null || outRefundNo == null){
			fail.put("orderNo transactionId totalFee refundFee outRefundNo", "都不能为空");
			return validateError(fail);
		}
		Result r = wxPayService.refundForService(hotelId,groupId,refundFee, totalFee,orderNo,outRefundNo, transactionId, flag);
		return  r;
	}
	/**
	 * 退款
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2018年2月6日 下午5:51:11 <br/>
	 */
	@RequestMapping("refund")
	@ResponseBody
	public Result refund(@RequestBody JSONObject jsonObject) throws Exception {
		String orderNo = jsonObject.getString("orderNo");
		String transactionId = jsonObject.getString("transactionId");
		String flag = jsonObject.getString("flag");
		BigDecimal totalFee = jsonObject.getBigDecimal("totalFee");
		BigDecimal refundFee = jsonObject.getBigDecimal("refundFee");
		/*校验*/
		Map<String, String> fail = new HashMap<>();
		if(orderNo==null || transactionId==null||totalFee==null ||refundFee==null){
			fail.put("orderNo transactionId totalFee refundFee", "都不能为空");
			return validateError(fail);
		}
		return  wxPayService.refund(refundFee, totalFee,orderNo, transactionId, flag);
	}
	/**
	 * 微信刷卡扫码支付查询
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @throws Exception 
	 * @Date 2018年1月18日 上午10:46:00 <br/>
	 */
	@RequestMapping("orderQuery")
	@ResponseBody
	public Result orderQuery(@RequestBody JSONObject jsonObject) throws Exception {
		Result result = new Result();
		String orderNo = jsonObject.getString("orderNo");
		Map<String, String> fail = new HashMap<>();
		if(orderNo==null ){
			fail.put("orderNo ", "不能为空");
			return validateError(fail);
		}
		Map resultMap = wxPayService.orderQuery(orderNo);
		String retrun_code = resultMap.get("return_code").toString();
		String result_code = resultMap.get("result_code").toString();
		Object err_code_des = resultMap.get("err_code_des");
		if(retrun_code.equals("SUCCESS")){
			if(result_code.equals("SUCCESS")){
				String trade_state = resultMap.get("trade_state").toString();
				Object trade_state_desc = resultMap.get("trade_state_desc");
				if(trade_state.toString().equals("SUCCESS")){
					result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
					result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
					result.setReturnCode(WxPayForScanReturnCodeEnum.PAY_SUCCESS.getCode());
					result.setReturnMsg(WxPayForScanReturnCodeEnum.PAY_SUCCESS.getDesc());
				}else if(trade_state.equals("USERPAYING")){
					result.setStatus(ReturnStatusEnum.FAILURE.getCode());
					result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
					result.setReturnCode(WxPayForScanReturnCodeEnum.USERPAYING.getCode());
					result.setReturnMsg(WxPayForScanReturnCodeEnum.USERPAYING.getDesc());
				}else{
					result.setStatus(ReturnStatusEnum.FAILURE.getCode());
					result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
					result.setReturnCode(WxPayForScanReturnCodeEnum.PAY_FAILURE.getCode());
					result.setReturnMsg(WxPayForScanReturnCodeEnum.PAY_FAILURE.getDesc());
					result.setData(trade_state_desc);
				}
			}else{
				result.setStatus(ReturnStatusEnum.FAILURE.getCode());
				result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
				result.setReturnCode(WxPayForScanReturnCodeEnum.QUERY_FAILURE.getCode());
				result.setReturnMsg(WxPayForScanReturnCodeEnum.QUERY_FAILURE.getDesc());
				result.setData(err_code_des);
			}
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(WxPayForScanReturnCodeEnum.PAY_FAILURE.getCode());
			result.setReturnMsg(WxPayForScanReturnCodeEnum.PAY_FAILURE.getDesc());
			result.setData(err_code_des);
			}
		return result;
	}
	
	/**
	 * 服务商微信刷卡扫码支付查询
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @throws Exception 
	 * @Date 2018年1月18日 上午10:46:00 <br/>
	 */
	@RequestMapping("orderQueryForService")
	@ResponseBody
	public Result orderQueryForService(@RequestBody JSONObject jsonObject) throws Exception {
		Result result = new Result();
		String orderNo = jsonObject.getString("orderNo");
		String hotelId = jsonObject.getString("hotelId");
		String groupId = jsonObject.getString("groupId");
		Map<String, String> fail = new HashMap<>();
		if(orderNo==null ){
			fail.put("orderNo ", "不能为空");
			return validateError(fail);
		}
		Map resultMap = wxPayService.orderQueryForService(hotelId,groupId,orderNo);
		String retrun_code = resultMap.get("return_code").toString();
		String result_code = resultMap.get("result_code").toString();
		Object err_code_des = resultMap.get("err_code_des");
		if(retrun_code.equals("SUCCESS")){
			if(result_code.equals("SUCCESS")){
				String trade_state = resultMap.get("trade_state").toString();
				Object trade_state_desc = resultMap.get("trade_state_desc");
				if(trade_state.toString().equals("SUCCESS")){
					result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
					result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
					result.setReturnCode(WxPayForScanReturnCodeEnum.PAY_SUCCESS.getCode());
					result.setReturnMsg(WxPayForScanReturnCodeEnum.PAY_SUCCESS.getDesc());
				}else if(trade_state.equals("USERPAYING")){
					result.setStatus(ReturnStatusEnum.FAILURE.getCode());
					result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
					result.setReturnCode(WxPayForScanReturnCodeEnum.USERPAYING.getCode());
					result.setReturnMsg(WxPayForScanReturnCodeEnum.USERPAYING.getDesc());
				}else{
					result.setStatus(ReturnStatusEnum.FAILURE.getCode());
					result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
					result.setReturnCode(WxPayForScanReturnCodeEnum.PAY_FAILURE.getCode());
					result.setReturnMsg(WxPayForScanReturnCodeEnum.PAY_FAILURE.getDesc());
					result.setData(trade_state_desc);
				}
			}else{
				result.setStatus(ReturnStatusEnum.FAILURE.getCode());
				result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
				result.setReturnCode(WxPayForScanReturnCodeEnum.QUERY_FAILURE.getCode());
				result.setReturnMsg(WxPayForScanReturnCodeEnum.QUERY_FAILURE.getDesc());
				result.setData(err_code_des);
			}
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(WxPayForScanReturnCodeEnum.PAY_FAILURE.getCode());
			result.setReturnMsg(WxPayForScanReturnCodeEnum.PAY_FAILURE.getDesc());
			result.setData(err_code_des);
		}
		return result;
	}
}
